<?php
//Contains general functions used througout the Website Both Admin & Fron End Section

//------------------------------------------------------------------------------
// Function to sanitize values received from the form. Prevents SQL injection
//
//   $str = String to be cleaned
//   $DB  = Connection to the DB
//	returns the Cleaned (sanitized) string
//------------------------------------------------------------------------------
function clean($str, $DB) {
    $str = @trim($str);
    if (get_magic_quotes_gpc ()) {
        $str = stripslashes($str);
    }
    return mysqli_real_escape_string($DB, $str);
}


//------------------------------------------------------------------------------
// Function to sanitize/avoid email injections. Prevents SQL injection
//
//   $str = String to be cleaned
//	returns the Cleaned (sanitized) string
//------------------------------------------------------------------------------
function heal($str) {
	$injections = array('/(\n+)/i',
	'/(\r+)/i',
	'/(\t+)/i',
	'/(%0A+)/i',
	'/(%0D+)/i',
	'/(%08+)/i',
	'/(%09+)/i'
	);
	$str= preg_replace($injections,'',$str);
	return $str;
}


//------------------------------------------------------------------------------
// Function to validate data based on a given regex pattern
//
//   $what = what you are checking (Phone, Email, etc)
//   $data = the string you want to check
//	returns TRUE if the given data is valid and FALSE if not.
//------------------------------------------------------------------------------

function isValid( $what, $data ) {

	switch( strtolower($what) ) {

		// Validate a phone number
		case 'phone':
			$pattern = "/^([1])?[0-9]{10}$/i";
		break;

		// Validate email address
		case 'email':
			$pattern = "/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/i";
		break;

                // Validate string only and spaces
		case 'stringonly':
			$pattern = '/^[A-Za-z\s ]+$/';
		break;

                // Validate username
		case 'username':
			// Alphabet, digit, _ and . are allow. Minimum 6 character. Maximum 30 characters
                        $pattern = '/^[a-zA-Z\d_.]{6,30}$/i';
		break;

                // Validate if it is any kind of number
                case 'alphanumeric':
                    return ctype_alnum ($data);
                break;

                // Validate if it is any kind of number
                case 'number':
                    return is_numeric ($data);
                break;

                // Validate if it is an integer
                case 'integer':
                    return ( is_integer($data) || is_int($data) );
                break;

                // Validate if it is an float/double
                case 'double':
                    return ( is_double($data) || is_float($data) );
                break;

                // Validate password
                case 'password':
                    // Alphabet, digit, _ and . are allow. Minimum 6 character. Maximum 30 characters
                    $pattern = '/^[a-zA-Z\d_.]{'. MIN_CHARS_USERNAME_PASSW .','. MAX_CHARS_USERNAME_PASSW .'}$/i';
                break;


		default:
			return false;
		break;

	}
	return preg_match($pattern, $data) ? true : false;
}


//------------------------------------------------------------------------------
// Delete a file or recursively delete a directory
//
//   $str = string $str Path to file or directory
//
//------------------------------------------------------------------------------

function recursiveDelete($str){
    if(is_file($str)){
        return @unlink($str);
    }
    elseif(is_dir($str)){
        $scan = glob(rtrim($str,'/').'/*');
        foreach($scan as $index=>$path){
            recursiveDelete($path);
        }
        return @rmdir($str);
    }
}

//------------------------------------------------------------------------------
// Delete all files of a directory
//
//   $str = string $str Path to the directory (e.g. 'your/directory/')
//
//------------------------------------------------------------------------------

function deleteAllFilesDirectory($str){
    foreach(glob($str.'*.*') as $v){
        unlink($v);
    }
}

//------------------------------------------------------------------------------
// Function to return the correspoding extension of a File
//
//   $str = File Name and Extension
//	returns File Extension or if there is none, ""
//------------------------------------------------------------------------------

function getFileExtension($str) {

        $i = strrpos($str,".");
        if (!$i) { return ""; }

        $l = strlen($str) - $i;
        $ext = substr($str,$i+1,$l);

        return $ext;

}

?>
